home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / LDB171.ARJ / MUTUAL.HPP < prev    next >
C/C++ Source or Header  |  1992-05-12  |  11KB  |  444 lines

  1. /*
  2.     mutual.hpp -- Loose Data Binder v 1.7:
  3.         mutually owned and persistence
  4.  
  5.     (C) Copyright 1992  John W. Small
  6.     All rights reserved
  7.  
  8.     PSW / Power SoftWare
  9.     P.O. Box 10072
  10.     McLean, Virginia 22102 8072 USA
  11.     (703) 759-3838
  12. */
  13.  
  14.  
  15. #ifndef Mutual_HPP
  16. #define Mutual_HPP
  17.  
  18.  
  19. #include <iostream.h>
  20. #include <iomanip.h>
  21.  
  22.  
  23. #ifndef Binder_HPP
  24. #include "binder.hpp"
  25. #endif
  26.  
  27.  
  28. #define ID_Mutual  0
  29. #define ID_MRef    ID_Mutual
  30. #define ID_MBinder 1
  31.  
  32.  
  33. typedef class Mutual * MutuaL;
  34. #define MutuaL0  ((MutuaL)0)
  35. typedef initVFTs initMutual;
  36. #define initVFTsEtc initVFTsOnly
  37.  
  38.  
  39. class Mutual  {
  40.  
  41.  
  42. private:
  43.  
  44.     friend    class StreamRegistry;
  45.     voiD      parenT;
  46.     unsigned  refCount, streamCount;
  47.     long      streamPos;
  48.  
  49.  
  50. protected:
  51.  
  52.     Mutual  (initMutual);
  53.     static  void serror(const char *msg, unsigned id);
  54.     virtual void error(const char *msg);
  55.     virtual void warn(const char *msg);
  56.     virtual void fput(ostream&)  {}
  57.     static  MutuaL fget(istream&, MutuaL InstancE)
  58.             { return InstancE; }
  59.  
  60.  
  61. public:
  62.  
  63.     static   int refDebug, streamDebug;
  64.     static   char memberTermChar;
  65.  
  66.     Mutual   (Mutual&)  { Mutual(initVFTsEtc); }
  67.     static   void RegisterClass(unsigned id,
  68.             MutuaL (*fgeT)(istream&, MutuaL));
  69.     virtual  int operator=(Mutual&)  { return 0; }
  70.     virtual  MutuaL clone() { return MutuaL0; }
  71.     voiD     ParenT()  { return parenT; }
  72.     virtual  unsigned ID()  { return ID_Mutual; }
  73.     virtual  unsigned restream();
  74.     void     unlink(voiD P = voiD0);
  75.     int      link(voiD P = voiD0);
  76.     unsigned RefCount()  { return refCount; }
  77.     long     StreamPos()  { return streamPos; }
  78.     virtual  ~Mutual()  {}
  79.  
  80. };
  81.  
  82.  
  83.  
  84. typedef class MBinder * MBindeR;
  85. #define MBindeR0  ((MBindeR)0)
  86.  
  87. class MBinder : Binder, public Mutual  {
  88.  
  89.  
  90. protected:
  91.  
  92.     MBinder  (initVFTs)
  93.             : Binder(initVFTsOnly),
  94.             Mutual(initVFTsEtc)  {}
  95.     Binder:: destruct;
  96.     virtual  voiD Dassign(voiD D, const voiD S);
  97.     virtual  voiD Dnew(const voiD D);
  98.     virtual  void Ddelete(voiD D);
  99.     virtual  int Dattach(voiD D)
  100.             { return ((MutuaL)D)->link(this); }
  101.     virtual  void Ddetach(voiD D)
  102.             { ((MutuaL)D)->unlink(this); }
  103.     Binder:: sberror;
  104.     Binder:: berror;
  105.     virtual  void Dstore(ostream& os, voiD D);
  106.     virtual  voiD Dload(istream& is);
  107.     Binder:: store;
  108.     static   MBindeR load(istream& is, MBindeR thiS);
  109.     Binder:: vload;
  110.     virtual  void fput(ostream& os)  { store(os); }
  111.     static   MutuaL fget(istream& is, MutuaL InstancE)
  112.             { return (MutuaL)
  113.             load(is,(MBindeR)InstancE); }
  114.  
  115.  
  116. public:
  117.  
  118.  
  119.  
  120. /*  Constructors and destructor  */
  121.  
  122.     MBinder (unsigned flags = BDR_DDELETE | BDR_DSTORE,
  123.         unsigned maxNodes = BDR_MAXNODES,
  124.         unsigned limit = BDR_LIMIT,
  125.         unsigned delta = BDR_DELTA) :
  126.         Binder(flags,maxNodes,limit,delta),
  127.         Mutual(initVFTsEtc)  {}
  128.     MBinder (MutuaL argv[],
  129.         unsigned argc = 0,
  130.         unsigned flags = BDR_BIND_ONLY)
  131.         : Binder((voiDV)argv,argc,flags),
  132.         Mutual(initVFTsEtc)  {}
  133.     MBinder (const char *filename)
  134.         : Binder(initVFTsOnly),
  135.         Mutual(initVFTsEtc)
  136.         { (void) vload(filename,(BDRsloaD)
  137.         MBinder::load,this); }
  138.     int     save(const char *filename)
  139.         { return ((!RefCount())? Binder::
  140.         save(filename) : 0); }
  141.     MutuaL* vector()
  142.             { return (MutuaL*)Binder::
  143.             vector(); }
  144.     virtual ~MBinder()  { MBinder::destruct(); }
  145.  
  146.  
  147.     static  void RegisterClass()
  148.         { Mutual::RegisterClass(ID_MBinder,
  149.         MBinder::fget); }
  150.     virtual unsigned ID()  { return ID_MBinder; }
  151.     virtual unsigned restream();
  152.  
  153.  
  154.  
  155. /*  Housekeeping Primitives  */
  156.  
  157.     Binder:: Limit;
  158.     Binder:: setLimit;
  159.     Binder:: pack;
  160.     Binder:: Delta;
  161.     Binder:: setDelta;
  162.     Binder:: Nodes;
  163.     Binder:: MaxNodes;
  164.     Binder:: setMaxNodes;
  165.     Binder:: vacancy;
  166.     Binder:: vacancyNonElastic;
  167.     Binder:: Flags;
  168.     Binder:: setFlags;
  169.     Binder:: resetFlags;
  170.     MBinder& operator<<(MBinder& (*manipulator)
  171.             (MBinder&)) { return 
  172.             (manipulator? (*manipulator)
  173.             (*this)    : *this); }
  174.  
  175.  
  176. /*  Elastic Array Primitives  */
  177.  
  178.     MutuaL   atIns(unsigned n, MutuaL D)
  179.             { return (MutuaL)Binder::
  180.             atIns(n,(voiD)D); }
  181.     MutuaL   atInsNew(unsigned n, const MutuaL D)
  182.             { return (MutuaL)Binder::
  183.             atInsNew(n,(const voiD)D); }
  184.     MutuaL   atRmv(unsigned n)
  185.             { return (MutuaL)Binder::
  186.             atRmv(n); }
  187.     Binder:: allRmv;
  188.     Binder:: atDel;
  189.     MutuaL   atDelAsg(unsigned n, MutuaL D)
  190.             { return (MutuaL)Binder::
  191.             atDelAsg(n,(voiD)D); }
  192.     Binder:: allDel;
  193.     MutuaL   atPut(unsigned n, MutuaL D)
  194.             { return (MutuaL)Binder::
  195.             atPut(n,(voiD)D); }
  196.     MutuaL   atPutNew(unsigned n, const MutuaL D)
  197.             { return (MutuaL)Binder::
  198.             atPutNew(n,(const voiD)D); }
  199.     MutuaL   atPutAsg(unsigned n, const MutuaL D)
  200.             { return (MutuaL)Binder::
  201.             atPutAsg(n,(const voiD)D); }
  202.     MutuaL   atGet(unsigned n)
  203.             { return (MutuaL)Binder::
  204.             atGet(n); }
  205.     MutuaL   operator[](unsigned n)
  206.             { return atGet(n); }
  207.     MutuaL   atGetAsg(unsigned n, MutuaL D)
  208.             { return (MutuaL)Binder::
  209.             atGetAsg(n,(voiD) D); }
  210.     MutuaL   atXchg(unsigned n, MutuaL D)
  211.             { return (MutuaL)Binder::
  212.             atXchg(n,(voiD) D); }
  213.     unsigned index(const MutuaL D)
  214.             { return Binder::
  215.             index((const voiD)D); }
  216.     Binder:: forEach;
  217.  
  218.  
  219. /*  Stack - Deque - Queue Primitives  */
  220.  
  221.     MutuaL   push(MutuaL D)
  222.             { return (MutuaL)Binder::
  223.             push((voiD) D); }
  224.     MutuaL   pushNew(const MutuaL D)
  225.             { return (MutuaL)Binder::
  226.             pushNew((const voiD)D); }
  227.     MutuaL   pop()
  228.             { return (MutuaL)Binder::
  229.             pop(); }
  230.     MBinder&  operator>>(MutuaL& D)
  231.             { D = atRmv(0); return *this; }
  232.     Binder:: popDel;
  233.     MutuaL   popDelAsg(MutuaL D)
  234.             { return (MutuaL)Binder::
  235.             popDelAsg((voiD)D); }
  236.     MutuaL   top()
  237.             { return (MutuaL)Binder::
  238.                 top(); }
  239.     MutuaL   topAsg(MutuaL D)
  240.             { return (MutuaL)Binder::
  241.             topAsg((voiD)D); }
  242.     MutuaL   insQ(MutuaL D)
  243.             { return (MutuaL)Binder::
  244.             insQ((voiD)D); }
  245.     MBinder&  operator<<(MutuaL D)
  246.             { atIns(Nodes(),D); return *this; }
  247.     MutuaL   insQNew(const MutuaL D)
  248.             { return (MutuaL)Binder::
  249.             insQNew((const voiD)D); }
  250.     MutuaL   unQ()
  251.             { return (MutuaL)Binder::
  252.             unQ(); }
  253.     Binder:: unQDel;
  254.     MutuaL   unQDelAsg(MutuaL D)
  255.             { return (MutuaL)Binder::
  256.             unQDelAsg((voiD)D); }
  257.     MutuaL   rear()
  258.             { return (MutuaL)Binder::
  259.                 rear(); }
  260.     MutuaL   rearAsg(MutuaL D)
  261.             { return (MutuaL)Binder::
  262.             rearAsg((voiD)D); }
  263.  
  264.  
  265. /*  List (single and double linked) Primitives  */
  266.  
  267.     Binder:: CurNode;
  268.     Binder:: setCurNode;
  269.     MutuaL   ins(MutuaL D)
  270.             { return (MutuaL)Binder::
  271.             ins((voiD)D); }
  272.     MutuaL   insNew(const MutuaL D)
  273.             { return (MutuaL)Binder::
  274.             insNew((const voiD)D); }
  275.     MutuaL   rmv()
  276.             { return (MutuaL)Binder::
  277.             rmv(); }
  278.     Binder:: del;
  279.     MutuaL   delAsg(MutuaL D)
  280.             { return (MutuaL)Binder::
  281.             delAsg((voiD)D); }
  282.     MutuaL   put(MutuaL D)
  283.             { return (MutuaL)Binder::
  284.             put((voiD)D); }
  285.     MutuaL   putNew(const MutuaL D)
  286.             { return (MutuaL)Binder::
  287.             putNew((const voiD)D); }
  288.     MutuaL   putAsg(const MutuaL D)
  289.             { return (MutuaL)Binder::
  290.             putAsg((const voiD)D); }
  291.     MutuaL   get()
  292.             { return (MutuaL)Binder::
  293.             get(); }
  294.     operator MutuaL()  { return get(); }
  295.     MutuaL   getAsg(MutuaL D)
  296.             { return (MutuaL)Binder::
  297.             getAsg((voiD)D); }
  298.     MutuaL   next()
  299.             { return (MutuaL)Binder::
  300.             next(); }
  301.     MutuaL   operator++()
  302.             { return next(); }
  303.     MutuaL   nextAsg(MutuaL D)
  304.             { return (MutuaL)Binder::
  305.             nextAsg((voiD)D); }
  306.     MutuaL   prev()
  307.             { return (MutuaL)Binder::
  308.             prev(); }
  309.     MutuaL   operator--()
  310.             { return prev(); }
  311.     MutuaL   prevAsg(MutuaL D)
  312.             { return (MutuaL)Binder::
  313.             prevAsg((voiD)D); }
  314.  
  315.     MutuaL   firstThat(BDRdetecT B, voiD M = voiD0)
  316.             { return (MutuaL)Binder::
  317.             firstThat(B,M); }
  318.     MutuaL   lastThat(BDRdetecT B, voiD M = voiD0)
  319.             { return (MutuaL)Binder::
  320.             lastThat(B,M); }
  321.  
  322.  
  323. /*  Priority Q, Set, Bag, Dictionary, Sort Primitives  */
  324.  
  325.     Binder:: Sorted;
  326.     Binder:: unSort;
  327.     Binder:: setComP;
  328.     Binder:: ComP;
  329.     Binder:: sort;
  330.     MutuaL   insSort(MutuaL D)
  331.             { return (MutuaL)Binder::
  332.             insSort((voiD)D); }
  333.     MutuaL   insSortNew(const MutuaL D)
  334.             { return (MutuaL)Binder::
  335.             insSortNew((const voiD)D); }
  336.     MutuaL   insUnique(MutuaL D)
  337.             { return (MutuaL)Binder::
  338.             insUnique((voiD)D); }
  339.     MutuaL   insUniqueNew(const MutuaL D)
  340.             { return (MutuaL)Binder::
  341.             insUniqueNew((const voiD)D); }
  342.     MutuaL   findFirst(const MutuaL K)
  343.             { return (MutuaL)Binder::
  344.             findFirst((const voiD)K); }
  345.     MutuaL   findNext(const MutuaL K)
  346.             { return (MutuaL)Binder::
  347.             findNext((const voiD)K); }
  348.     MutuaL   findLast (const MutuaL K)
  349.             { return (MutuaL)Binder::
  350.             findLast((const voiD)K); }
  351.     MutuaL   findPrev(const MutuaL K)
  352.             { return (MutuaL)Binder::
  353.             findPrev((const voiD)K); }
  354.     unsigned findAll(const MutuaL K)
  355.             { return Binder::
  356.             findAll((const voiD)K); }
  357.  
  358.  
  359.  
  360. };    /*  class MBinder  */
  361.  
  362.  
  363.  
  364. typedef void (*GenericFnC)();
  365. #define GenericFnC0  ((GenericFnC)0)
  366. #define ID_UnknownGenericFnC  0
  367.  
  368. class StreamRegistry  {
  369.  
  370.  
  371. private:
  372.  
  373.     Binder ClassRecords;
  374.     Binder InstanceRecords;
  375.     Binder FunctionRecords;
  376.  
  377.  
  378. protected:
  379.  
  380.     void CIerror(char *msg, unsigned id = 0,
  381.         MutuaL InstancE = MutuaL0);
  382.     void CIwarn(char *msg, unsigned id = 0,
  383.         MutuaL InstancE = MutuaL0);
  384.     void FNCerror(char *msg, unsigned id);
  385.     void FNCwarn(char *msg, unsigned id);
  386.  
  387. public:
  388.  
  389.     static int debug;
  390.  
  391.     StreamRegistry() :
  392.         ClassRecords(BDR_DDELETE),
  393.         InstanceRecords(BDR_DDELETE),
  394.         FunctionRecords(BDR_DDELETE)
  395.         {}
  396.     unsigned restream();
  397.     void RegisterClass(unsigned id,
  398.         MutuaL (*fgeT)(istream&, MutuaL));
  399.     void registerFunction(unsigned id, GenericFnC fnC);
  400.     GenericFnC fnCLU(unsigned id);
  401.     unsigned fnCID(GenericFnC fnC);
  402.     void forgetRegistrations();
  403.     void fput(ostream& os, Mutual& Instance);
  404.     void fget(istream& is, MutuaL& InstancE);
  405.     ~StreamRegistry()  {}
  406.  
  407. };
  408.  
  409.  
  410. extern StreamRegistry StreamReg;
  411.  
  412. #define Restream()  StreamReg.restream()
  413. #define RegisterFunction(id,fnC)  \
  414.     StreamReg.registerFunction(id,(GenericFnC)fnC)
  415. #define fnC2ID(fnC)  StreamReg.fnCID((GenericFnC)fnC)
  416. #define ID2fnC(fnCType,id)  (fnCType) StreamReg.fnCLU(id)
  417. #define ForgetRegistrations()  \
  418.     StreamReg.forgetRegistrations()
  419.  
  420. inline ostream& operator<<(ostream& os, Mutual& m)
  421. {
  422.     StreamReg.fput(os,m);
  423.     return os;
  424. }
  425.  
  426. inline istream& operator>>(istream& is, MutuaL& M)
  427. {
  428.     StreamReg.fget(is,M);
  429.     return is;
  430. }
  431.  
  432. inline ostream& operator<<(ostream& os, MBinder& b)
  433. {
  434.     if (b.Flags(BDR_DSTORE))
  435.         os << *(MutuaL)&b;
  436.     return os;
  437. }
  438.  
  439. extern ostream& Mendm(ostream& os);
  440. extern istream& Mnextm(istream& is);
  441.  
  442.  
  443. #endif  /* Mutual_HPP */
  444.